//
//  UITextField+Validate.m
//  MobileCRM
//
//  Created by winter on 16/2/25.
//  Copyright © 2016年 江苏金恒信息科技有限公司. All rights reserved.
//

#import "UITextField+Validate.h"
#import <objc/runtime.h>
#import "NSString+Util.h"
#import "UIColor+Util.h"
char const *validateRegexKey = "validateRegExKey";
char const *validateRegexBgKey = "validateRegExBackgroundKey";


@implementation UITextField(Validate)


- (BOOL)validateValueByRegEx:(NSString *)regEx
{
    NSString *val = self.text;
    return [val validateByRegEx:regEx];
}

- (NSString *)validateValueByAssociatedRegex;
{
    NSDictionary *regs = objc_getAssociatedObject(self, validateRegexKey);
    NSString *str = nil;
    BOOL correct = NO;
    NSString *val = [self.text wsStringByTrimmingWhiteSpace];
    if([regs valueForKey:wsValidateRequired])
    {
        BOOL required = [[regs valueForKey:wsValidateRequired] boolValue];
        correct = [val validateByRegEx:@".+"];
        if(required)
        {
            if(!correct)
            {
                [self refreshStatusAfterValidate];
                return wsValidateRequireERROR;
            }
        }
    }
    if(!correct)
    {
        val = nil;
        regs = nil;
       return nil;
    }
    if([regs valueForKey:wsValidateMinLength])
    {
        int length = (int)[[regs valueForKey:wsValidateMinLength] integerValue];
        length = length<0?0:length;
        if(![val validateByRegEx:[NSString stringWithFormat:@".{%d,}",length]])
        {
            [self refreshStatusAfterValidate];
            return wsValidateMinLengthERROR;
        }
    }
    if([regs valueForKey:wsValidateMaxLength])
    {
        int length = (int)[[regs valueForKey:wsValidateMaxLength] integerValue];
        length = length<0?0:length;
        if(![val validateByRegEx:[NSString stringWithFormat:@".{%d,}",length]])
        {
            [self refreshStatusAfterValidate];
            return wsValidateMaxLengthERROR;
        }
    }
    if([regs valueForKey:wsValidateValueRegex])
    {
        if(![val validateByRegEx:[regs valueForKey:wsValidateValueRegex]])
        {
            [self refreshStatusAfterValidate];
            return wsValidateValueRegexERROR;
        }
    }
    
    if([regs valueForKey:wsValidateMobile])
    {
        BOOL result = [[regs valueForKey:wsValidateMobile] boolValue];
        if(!result)return nil;
        if(![val validateByRegEx:wsValidateMobileRegex])
        {
            [self refreshStatusAfterValidate];
            return wsValidateMobileError;
        }
    }
    
    if([regs valueForKey:wsValidateEmail])
    {
        BOOL result = [[regs valueForKey:wsValidateEmail] boolValue];
        if(!result)return nil;
        if(![val validateByRegEx:wsValidateEmailRegex])
        {
            [self refreshStatusAfterValidate];
            return wsValidateEmailError;
        }
    }
    [self RestoreStatusAfterValidate];
    return str;
}

- (void)refreshStatusAfterValidate
{
    id oldColor = objc_getAssociatedObject(self, validateRegexBgKey);
    if(oldColor)
    {
        self.backgroundColor = [UIColor colorWithHexString:@"#ffcccc"];
    }
    else
    {
        objc_setAssociatedObject(self, validateRegexBgKey, self.backgroundColor, OBJC_ASSOCIATION_COPY_NONATOMIC);
        self.backgroundColor = [UIColor colorWithHexString:@"#ffcccc"];
        //if(self.backgroundColor)
        //else
            //objc_setAssociatedObject(self, validateRegexBgKey, self.backgroundColor, OBJC_ASSOCIATION_COPY_NONATOMIC);
    }
}
- (void)RestoreStatusAfterValidate
{
    id oldColor = objc_getAssociatedObject(self, validateRegexBgKey);
    if(oldColor)
    {
        self.backgroundColor = (UIColor *)oldColor;
    }
}

/*
- (void)addValidateRegEx:(NSString *)regEx
{
    NSArray *regExes = objc_getAssociatedObject(self, VALIDATEREGEXKEY);
    if(regExes)
    {
        NSMutableArray *newRegEx = [NSMutableArray arrayWithArray:regExes];
        [newRegEx addObject:regEx];
        objc_setAssociatedObject(self, VALIDATEREGEXKEY, newRegEx, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    else
    {
        objc_setAssociatedObject(self, VALIDATEREGEXKEY, @[regEx], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
}
- (void)addValidateRegExes:(NSArray *)regExes
{
    NSArray *oRegExes = objc_getAssociatedObject(self, VALIDATEREGEXKEY);
    if(oRegExes)
    {
        NSMutableArray *newRegEx = [NSMutableArray arrayWithArray:regExes];
        [newRegEx addObjectsFromArray:oRegExes];
        objc_setAssociatedObject(self, VALIDATEREGEXKEY, newRegEx, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    else
    {
        objc_setAssociatedObject(self, VALIDATEREGEXKEY, regExes, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
}
 */

- (void)addValidateRegEx:(NSDictionary *)regEx
{
    NSDictionary *oRegExes = objc_getAssociatedObject(self, validateRegexKey);
    if(oRegExes)
    {
        NSMutableDictionary *regs = [NSMutableDictionary dictionaryWithDictionary:oRegExes];
        [regs addEntriesFromDictionary:regEx];
        objc_setAssociatedObject(self, validateRegexKey, regs, OBJC_ASSOCIATION_COPY_NONATOMIC);
    }
    else
    {
        objc_setAssociatedObject(self, validateRegexKey, regEx, OBJC_ASSOCIATION_COPY_NONATOMIC);
    }
}

- (void)removeValidateRegExes
{
    id regExes = objc_getAssociatedObject(self, validateRegexKey);
    if(regExes)
    {
        objc_removeAssociatedObjects(regExes);
    }
    id oldColor = objc_getAssociatedObject(self, validateRegexBgKey);
    if(oldColor)
    {
        objc_removeAssociatedObjects(oldColor);
    }
}


@end
